package com.lagou.demo.interceptor.impl;


import com.lagou.demo.interceptor.HandlerInterceptor;
import com.lagou.edu.mvcframework.annotations.LagouService;
import com.lagou.edu.mvcframework.pojo.Handler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * 自定义springmvc拦截器
 */
@LagouService
public class MyInterceptor implements HandlerInterceptor {


    /**
     * 会在handler方法业务逻辑执行之前执行
     * 往往在这里完成权限校验工作
     * @param request
     * @param response
     * @param handler
     * @return  返回值boolean代表是否放行，true代表放行，false代表中止
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String username = request.getParameter("username");
        String [] authority = ((Handler) handler).getAuthority();
        boolean support = Arrays.asList(authority).contains(username);
        System.out.println("MyInterceptor preHandle......");
        return support;
    }



}
